import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:myapp/app/data/repository/comm_repository.dart';
import 'package:myapp/app/routes/app_pages.dart';

class LoginController extends GetxController {

  final _commRepository = Get.find<CommRepository>();

  final count = 0.obs;

  final userName = "".obs;
  final password = "".obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {}
  void increment() => count.value++;

  void pressLogin() {
    if(userName.isEmpty || password.isEmpty) {
      Fluttertoast.showToast(msg: "请输入账号密码");
      return;
    }
    Get.defaultDialog(title: "正在登录...", content: CircularProgressIndicator());
    _login();
  }

  void _login() async {
    try {
      await _commRepository.login(userName.value.trim(), password.value.trim());
      Fluttertoast.showToast(msg: "登录成功");
      //关闭dialog
      Get.back();
      Get.offAndToNamed(Routes.HOME);
    } catch(e) {
      print("login_error $e");
      Get.back();
      Fluttertoast.showToast(msg: e.toString());
    }
  }
}
